﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace Administrare
{
    public partial class AplicatiiFrm : FrmRezumat
    {

        public DataSet result = new DataSet();
        public BindingSource bs = new BindingSource();

        public AplicatiiFrm(ModuleClass mInfo)
            : base(mInfo)
        {
            InitializeComponent();
            workConnString = "AdminConnectionString";
            adminDataSet.aspnet_Applications.ApplicationIdColumn.DefaultValue = Guid.NewGuid();
            adminDataSet.aspnet_Applications.LoweredApplicationNameColumn.DefaultValue = String.Empty;
            adminDataSet.aspnet_Applications.SystemColumn.DefaultValue = false;

            result.Tables.Add("Metode");
            result.Tables["Metode"].Columns.Add("nume_metoda");
            result.Tables["Metode"].Columns.Add("idModul");
            bs.DataSource = result;
            bs.DataMember = "Metode";

            this.AssamblyNameColumn.DataSource = bs;
            this.AssamblyNameColumn.DisplayMember = "nume_metoda";
            this.AssamblyNameColumn.ValueMember = "nume_metoda";

        }

        private void AplicatiiFrm_Load(object sender, EventArgs e)
        {
            AddBindingSource(aspnet_ApplicationsBindingSource);
            listTableAdaptor.Add(this.aspnet_ApplicationsTableAdapter.Adapter);

            SqlConnection con = DeschideConexiune(workConnString);
            this.aspnet_ApplicationsTableAdapter.Connection = con;
            this.aspnet_ApplicationsTableAdapter.Fill(this.adminDataSet.aspnet_Applications);
            InchideConexiune(con);
            FrmMainMdi frm = MdiParent as FrmMainMdi;
            frm.GetModuleSelectList(ref result);
            HandleSalvare += Salvare;

        }

        private void aspnet_ApplicationsDataGridView_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
        {
            e.Row.Cells[ApplicationIdColumn.Index].Value = Guid.NewGuid(); 
        }

        override public void Salvare(object sender, EventArgs args)
        {
            base.Salvare(this, args);
            if (EroareSalvare)
                return;
            AdminDataSet.aspnet_ApplicationsRow rd = ((DataRowView)aspnet_ApplicationsBindingSource.Current).Row as AdminDataSet.aspnet_ApplicationsRow;
            Guid cGui = rd.ApplicationId;
            SqlConnection con = DeschideConexiune(workConnString);
            this.aspnet_ApplicationsTableAdapter.Connection = con;
            this.aspnet_ApplicationsTableAdapter.Fill(this.adminDataSet.aspnet_Applications);
            InchideConexiune(con);
            int poz = aspnet_ApplicationsBindingSource.Find("ApplicationId", cGui);
            if (poz > 0)
                aspnet_ApplicationsBindingSource.Position = poz;
        }

        private void aspnet_ApplicationsDataGridView_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == ApplicationNameColumn.Index)
            {
                if (aspnet_ApplicationsDGV[LoweredApplicationNameColumn.Index, e.RowIndex].Value == null)
                    return;
                if (aspnet_ApplicationsDGV[LoweredApplicationNameColumn.Index, e.RowIndex].Value.ToString()==String.Empty)
                    aspnet_ApplicationsDGV[LoweredApplicationNameColumn.Index, e.RowIndex].Value = 
                        aspnet_ApplicationsDGV[e.ColumnIndex,e.RowIndex].EditedFormattedValue.ToString().ToLower();
            }
        }

        private void aspnet_ApplicationsDGV_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            if  (aspnet_ApplicationsDGV[ApplicationNameColumn.Index, e.RowIndex].EditedFormattedValue.ToString() == String.Empty)
                return;
            if (aspnet_ApplicationsDGV[DescriptionColumn.Index,e.RowIndex].EditedFormattedValue.ToString() == String.Empty)
            {
                e.Cancel = true;
                MessageBox.Show("Descrierea modului este obligatorie!", "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (aspnet_ApplicationsDGV[MenuNameColumn.Index, e.RowIndex].EditedFormattedValue.ToString() == String.Empty)
            {
                e.Cancel = true;
                MessageBox.Show("Meniul principal al modului este obligatoriu!", "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (aspnet_ApplicationsDGV[AssamblyNameColumn.Index, e.RowIndex].EditedFormattedValue.ToString() == String.Empty)
            {
                if (e.ColumnIndex == AssamblyNameColumn.Index)
                    e.Cancel = true;
                MessageBox.Show("Va rugam selectati un biblioteca de functii (*.dll)!", "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

         }

        private void aspnet_ApplicationsDGV_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            foreach (DataGridViewRow dc in aspnet_ApplicationsDGV.Rows)
            {
                Boolean isSys = true;
                if (dc.Cells["SystemColumn"].Value != DBNull.Value)
                    isSys = Convert.ToBoolean(dc.Cells["SystemColumn"].Value);
                if (isSys)
                {
                    aspnet_ApplicationsDGV.Rows[dc.Index].DefaultCellStyle.BackColor = Color.LightBlue;
                    //aspnet_ApplicationsDGV[AssamblyNameColumn.Index, dc.Index].ReadOnly = true;
                    aspnet_ApplicationsDGV[AssamblyNameColumn.Index, dc.Index].Style.BackColor = Color.LightGray;
                }
            }

        }

        private void aspnet_ApplicationsBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            if (aspnet_ApplicationsBindingSource.Current == null)
                return;
            AdminDataSet.aspnet_ApplicationsRow aar = ((DataRowView)aspnet_ApplicationsBindingSource.Current).Row
                as AdminDataSet.aspnet_ApplicationsRow;
            EnableMainDeleteButton = !aar.System;
        }

        private void aspnet_ApplicationsDGV_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
                return;
            if (e.ColumnIndex == AssamblyNameColumn.Index)
            { 
                String nVal = ((DataRowView)bs.Current).Row["idModul"].ToString();
                 if (aspnet_ApplicationsDGV[IDInternColumn.Index, e.RowIndex].Value.ToString()!=nVal)
                    aspnet_ApplicationsDGV[IDInternColumn.Index, e.RowIndex].Value = nVal;
                
            }
        }
    }
}
